/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-11-4
 * @Time  : 下午3:21
 * @brief :
 */
var createUi = require('common:widget/ui/base/base.js').createUi,
    Dialog = require('common:widget/ui/dialog/dialog.js').Dialog;


exports.IdVerify = createUi({
    _data:{
        onConfirm:function(callback){},
        hasIdVerify:false,  //是否做了验证
        isIdVerify:false    //是否身份验证成功
    },

    _create:function(){
        var idVerify = __inline("./idverify.tmpl");
        var tpl = idVerify({gid:this.gid});

        var dialog = new Dialog({
            title:'实名认证',
            confirmBtn:true,
            width:500,
            content:tpl
        });
        this.idVerify();
        this.data({
            'dialog':dialog,
            '$root':$("#"+this.gid)
        })
    },


    _init:function(){
        var dialog = this.data('dialog'),
            $root = this.data('$root'),
            $tip = $root.find('.tip'),
            me = this;
        dialog.on("confirm",function(callback){
            if(me.data('hasIdVerify')){
                var id_card      = $root.find('.id_card').val(),
                    re_id_card   = $root.find('.re_id_card').val(),
                    id_card_name = $root.find('.id_card_name').val();

                $tip.text("");

                if(!id_card_name) {
                    $tip.text('请输入姓名。');
                    return;
                }
                if(!id_card) {
                    $tip.text('请输入身份证号码。');
                    return;
                }
                if(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(id_card) == false) {
                    $tip.text('请输入正确的身份证号');
                    return;
                }
                if(!re_id_card) {
                    $tip.text('请再次确认身份证号。');
                    return;
                }
                if(re_id_card != id_card) {
                    $tip.text('2次输入的身份证号不一致，请确认。');
                    return;
                }

                $.post("/account/setidcard",{
                    id_card_name:id_card_name,
                    id_card:id_card
                },function(ret,status){
                    if(ret.error_no == 0){
                        $tip.text('实名认证成功');
                        me.data("isIdVerify",true);
                        setTimeout(function(){
                            callback();
                        },1000);
                    }else{
                        $tip.text(ret.error_message);
                    }
                    me.data('hasIdVerify',true);
                },"json");
            }
        });
    },

    //检测实名认证
    check:function(){
        if(this.data('isIdVerify')){
            return true;
        }else{
            this.show();
            return false;
        }
    },

    //获取实名认证的状态
    idVerify:function(){
        var me = this;
        $.get("/account/getuserstatus",function(ret,status){
            if(ret.error_no==0 && ret.data.id_card){
                me.data('isIdVerify',true);
            }
            me.data('hasIdVerify',true)
        },"json");
    },

    show:function(){
       this.data("dialog").show();
    }
});